package com.cc2019.buke.user.Activity.address;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.cc2019.buke.R;
import com.cc2019.buke.user.Activity.RegisterActivity;
import com.cc2019.buke.user.adapter.AddressAdapter;
import com.cc2019.buke.user.entry.Address;
import com.cc2019.buke.user.tools.myDatabaseHelper;

import java.util.ArrayList;
import java.util.List;

public class AddressActivity extends AppCompatActivity implements View.OnClickListener{
    private Button add;
    private TextView show;
    private List<Address> addressList=new ArrayList<Address>();
    private myDatabaseHelper dbHelper;
    private ListView listView;
    private AddressAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);



        //setContentView(R.layout.activity_address);
        dbHelper = myDatabaseHelper.getInstance(this);
        initAddress();
        adapter=new AddressAdapter(AddressActivity.this,R.layout.address_item,addressList);
        add=(Button) findViewById(R.id.add_address_img);


        //跳转到添加地址界面
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AddressActivity.this, AddAddressInfoActivity.class);
                intent.putExtra("haveData","false");
                startActivity(intent);
            }
        });

        show= findViewById(R.id.show_address);
        show.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
                final Address address=addressList.get(position);
                AlertDialog.Builder builder=new AlertDialog.Builder(AddressActivity.this);
                LayoutInflater layoutInflater=LayoutInflater.from(AddressActivity.this);
                final View textEntryView=layoutInflater.inflate(R.layout.address_info_layout,null);//加载Alert
                builder.setView(textEntryView);
                builder.setTitle("选择相关操作:");

                Button delete_Info=(Button) textEntryView.findViewById(R.id.address_info_delete);
                delete_Info.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder delete_builder=new AlertDialog.Builder(AddressActivity.this);
                        delete_builder.setTitle("正在进行删除地址操作!");
                        delete_builder.setNegativeButton("取消",null);
                        delete_builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SQLiteDatabase db=dbHelper.getWritableDatabase();
                                db.execSQL("delete from address where name=? and phone=?",new String[]{address.getName(),address.getPhone()});
                                addressList.remove(position);
                                adapter.notifyDataSetChanged();//刷新列表
                            }
                        });
                        delete_builder.create().show();
                    }
                });

                //修改地址
                Button update_info=(Button)textEntryView.findViewById(R.id.address_info_update);
                update_info.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(AddressActivity.this, AddAddressInfoActivity.class);
                        intent.putExtra("haveData", "true");
                        intent.putExtra("name", address.getName());
                        intent.putExtra("phone", address.getPhone());
                        intent.putExtra("address", address.getAddress());
                        startActivity(intent);
                    }
                });
                builder.create().show();
            }
        });

    }

    private void initAddress() {
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from address ",null);
        while (cursor.moveToNext()){
            String name=cursor.getString(cursor.getColumnIndex("name"));
            String id=cursor.getString(cursor.getColumnIndex("id"));
            String phone=cursor.getString(cursor.getColumnIndex("phone"));
            String address=cursor.getString(cursor.getColumnIndex("address"));
            addressList.add(new Address(name,id,phone,address));
        }
        cursor.close();
    }


    @Override
    public void onClick(View v) {

        }

}

